package com.apexsoft.front.common.utils.httprequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 输入包
 * @author dinglei
 *
 */
public final class InPacket {
	
	private JSONObject params;
	
	public InPacket(){
		params = new JSONObject();
	}
	
	public InPacket(String jsonStr){
		this.params = JSON.parseObject(jsonStr);
	}
	
	public InPacket(JSONObject jsonObj){
		this.params = jsonObj;
	}
	
	public InPacket remove(Object key){
		this.params.remove(key);
		return this;
	}
	
	public InPacket set(Object key,Object value){	
		this.params.put(key.toString(), value);
		return this;
	}
	
	public InPacket set(Object[] keys,Object[] values){
		if(keys!=null && values!=null && keys.length==values.length){
			for(int i=0;i<keys.length;i++){
				Object key = keys[i];
				Object value = values[i];
				this.params.put(key.toString(), value);
			}
		}
		return this;
	}
	
	public void initialization(String jsonData){
		params = JSON.parseObject(jsonData);
	}
	
	public Object get(String key){
		return this.params.get(key);
	}
	
	public String getString(String key){
		return this.params.getString(key);
	}
	
	public String getString(String key,String defaultValue){
		if(this.params.containsKey(key))
			return this.params.getString(key);
		else
			return defaultValue;
	}
	
	public int getInt(String key){
		return this.params.getIntValue(key);
	}
	
	public int getInt(String key,int defaultValue){
		if(this.params.containsKey(key))
			return this.params.getIntValue(key);
		else
			return defaultValue;
	}
	
	public double getDouble(String key){
		return this.params.getDoubleValue(key);
	}
	
	public double getDouble(String key,double defaultValue){
		if(this.params.containsKey(key))
			return this.params.getDoubleValue(key);
		else
			return defaultValue;
	}
	
	public boolean getBoolean(String key){
		return this.params.getBooleanValue(key);
	}
	
	public boolean getBoolean(String key,boolean defaultValue){
		if(this.params.containsKey(key))
			return this.params.getBooleanValue(key);
		else
			return defaultValue;
	}
	
	public JSONObject getJSONObject(String key){
		return this.params.getJSONObject(key);
	}
	
	public JSONArray getJSONArray(String key){
		return this.params.getJSONArray(key);
	}
	
	public long getLong(String key){
		return this.params.getLongValue(key);
	}
	
	public long getLong(String key,long defaultValue){
		if(this.params.containsKey(key))
			return this.params.getLongValue(key);
		else
			return defaultValue;
	}
	
	public String toString(){
		return this.params.toJSONString();
	}
	
	public JSONObject toJson(){
		return this.params;
	}
	
}
